function formatDate(date, fmt, type = '1') {
	if (type !== '1') {
		date = new Date(new Date(date).getTime() - 8 * 3600 * 1000)
	}
	var o = {
		"M+": date.getMonth() + 1, //月份
		"d+": date.getDate(), //日   
		"h+": date.getHours(), //小时   
		"m+": date.getMinutes(), //分   
		"s+": date.getSeconds(), //秒   
		"q+": Math.floor((date.getMonth() + 3) / 3), //季度   
		"S": date.getMilliseconds() //毫秒   
	};
	if (/(y+)/.test(fmt))
		fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
	for (var k in o)
		if (new RegExp("(" + k + ")").test(fmt))
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
	return fmt;
}

function formatDate2(date) {
	var json_date = new Date(date).toJSON();
	console.log(new Date(new Date(json_date) + 8 * 3600 * 1000).toISOString())
	let creationTimeStr = new Date(new Date(json_date) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '') 
	return creationTimeStr.substring(0, 10)
}

function formatTime(time) {
	if (typeof time !== 'number' || time < 0) {
		return time
	}

	var hour = parseInt(time / 3600)
	time = time % 3600
	var minute = parseInt(time / 60)
	time = time % 60
	var second = time

	return ([hour, minute, second]).map(function(n) {
		n = n.toString()
		return n[1] ? n : '0' + n
	}).join(':')
}

function getWeekDay(timestamp){
	let day = new Date(timestamp).getDay()
	let weeks = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
	return weeks[day]
}
function transformTime(time) {
	if(time){
	  if(time.length){
		let timer=time.substring(0,10);
		let formet=timer.replace(/-/g,'/');
		return formet
	  }
  
	}
  
  
	
  
  }

function distance(pos1 = {}, pos2 = {}) {
	var a = Rad(pos1.lat) - Rad(pos2.lat);
	var b = Rad(pos1.lon) - Rad(pos2.lon);
	var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(Rad(pos1.lat)) * Math.cos(Rad(pos2.lat)) *
		Math.pow(Math.sin(b / 2), 2)));
	s = s * 6378.137;
	s = Math.round(s * 10000) / 10000; //输出公里
	s = s.toFixed(2)
	return s
}

function Rad(d) {
	return d * Math.PI / 180.0; //经纬度转换成三角函数中度分表形式。
}

var dateUtils = {
	UNITS: {
		'年': 31557600000,
		'月': 2629800000,
		'天': 86400000,
		'小时': 3600000,
		'分钟': 60000,
		'秒': 1000
	},
	humanize: function(milliseconds) {
		var humanize = '';
		for (var key in this.UNITS) {
			if (milliseconds >= this.UNITS[key]) {
				humanize = Math.floor(milliseconds / this.UNITS[key]) + key + '前';
				break;
			}
		}
		return humanize || '刚刚';
	},
	format: function(dateStr) {
		var date = dateStr && this.parse(dateStr) || Date.now() + 1000
		var diff = Date.now() - date.getTime();
		if (diff < this.UNITS['天']) {
			return this.humanize(diff);
		}
		var _format = function(number) {
			return (number < 10 ? ('0' + number) : number);
		};
		return date.getFullYear() + '/' + _format(date.getMonth() + 1) + '/' + _format(date.getDate()) + ' ' +
			_format(date.getHours()) + ':' + _format(date.getMinutes());
	},
	parse: function(str) { //将"yyyy-mm-dd HH:MM:ss"格式的字符串，转化为一个Date对象
		var a = str.split(/[^0-9]/);
		return new Date(a[0], parseInt(a[1]) - 1, a[2], a[3], a[4], a[5]);
	}
};

var objectUtils = {
	filterNull: function(obj) {
		var keys = Object.keys(obj)
		keys.map(v => {
			if (obj[v] == null) {
				delete obj[v]
			}
		})
		return obj
	}
}

/**
 * Created by PanJiaChen on 16/11/18.
 */

/**
 * @param {string} path
 * @returns {Boolean}
 */
function isExternal(path) {
	return /^(https?:|mailto:|tel:)/.test(path)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
function validUsername(str) {
	const valid_map = ['admin', 'editor']
	return valid_map.indexOf(str.trim()) >= 0
}

/**
 * @param {string} url
 * @returns {Boolean}
 */
function validURL(url) {
	const reg =
		/^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
	return reg.test(url)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
function validLowerCase(str) {
	const reg = /^[a-z]+$/
	return reg.test(str)
}


function urlParamsToObj(params = '') {
	var obj = {}; //声明对象
	params = params && params.split('&') || []
	for (var i = 0; i < params.length; i++) {
		var param = params[i].split("=");
		obj[param[0]] = param[1]; //为对象赋值
	}
	return obj;
}

function objToUrlParams(obj = {}) {
	let params = ''
	Object.keys(obj).map(v => {
		params += v + '=' + obj[v] + '&'
	})
	return params
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
function validUpperCase(str) {
	const reg = /^[A-Z]+$/
	return reg.test(str)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
function validAlphabets(str) {
	const reg = /^[A-Za-z]+$/
	return reg.test(str)
}

/**
 * @param {string} email
 * @returns {Boolean}
 */
function validEmail(email) {
	const reg =
		/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
	return reg.test(email)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
function isString(str) {
	if (typeof str === 'string' || str instanceof String) {
		return true
	}
	return false
}

/**
 * @param {Array} arg
 * @returns {Boolean}
 */
function isArray(arg) {
	if (typeof Array.isArray === 'undefined') {
		return Object.prototype.toString.call(arg) === '[object Array]'
	}
	return Array.isArray(arg)
}

module.exports = {
	formatTime,
	formatDate,
	formatDate2,
	dateUtils,
	objectUtils,
	distance,
	isArray,
	isString,
	urlParamsToObj,
	objToUrlParams,
	getWeekDay,
	transformTime
}
